<template>
  <div class="row m-0">
    <div v-for="column in columnList" :key="column._id" class="col-4 mb-4">
      <!--shadow-sm小阴影-->
      <div class="card h-100 shadow-sm" style="width: 18rem">
        <div class="card-body text-center">
          <img :src="column.avatar && column.avatar.url" class="rounded-circle border border-light w-25 my-3" alt="...">
          <h5 class="card-title">{{ column.title }}</h5>
          <p class="card-text">{{ column.description }}</p>
          <router-link :to="`/column/${column._id}`" class="btn btn-primary">进入专栏</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import {computed, defineComponent, PropType} from "vue";
import { ColumnProps } from "../store";

//defineComponent最重要的是：在TypeScript下，给予了组件 正确的参数类型推断
export default defineComponent({
  name:'ColumnList',
  props: {
    list: {
      //给 list一一匹配
      type: Array as PropType<ColumnProps[]>,//类型断言
      required: true // 属于一个必填项
    }
  },

  setup(props) {
    // 默认图片
    const columnList = computed(() => {
      return props.list.map(column => {
        if (!column.avatar) {
          // column.avater = require('@/assets/logo.png')
          column.avatar ={
            url:require('@/assets/logo.png')
          }
        }
        return column
      })
    })
    return {
      columnList
    }
  }
})
</script>